import Vuex from 'vuex';

export default function () {
    return new Vuex.Store({
        state () {
            return {
                userData: {
                    username: '',
                    signature: '',
                    profile: {
                        pos: {
                            x: 0,
                            y: 0,
                        },
                        scale: '0',
                        url: ''
                    },
                    friendReq: [],
                    friends: [],
                    currentMessages: [],
                },

                messageActive: [],

                chatData: {
                    mID: '',
                    mLimit: 15,
                    totalLength: 0,
                },
                chatTarget: {},
                chatMessage: [],
                imgQueue: [],
            }
        },
        mutations: {
            updateUser(state, target) {
                state.userData = target;
            },

            initMessageActive(state) {
                //为消息作active数组
                state.messageActive = new Array(length).fill(state.userData.currentMessages.length);
            },

            updateMessageActive(state, index) {
                state.messageActive.fill(false)[index] = true;
            },

            updateChatData(state, target) {
                state.chatData = target;
            },

            updateChatTarget(state, target) {
                state.chatTarget = target;
            },

            updateChatMessage(state, target) {
                state.chatMessage = target;
            },

            //消息中的图片列表
            updateChatMessageImgQueue(state, target) {
                state.imgQueue = target;
            },

            clearUnread(state, index) {
                state.userData.currentMessages[index].unread = 0;
            },

            addFriReq(state, target) {
                state.userData.friendReq.unshift(target);
            },

            deleteFriReq(state, index) {
                state.userData.friendReq.splice(index, 1);
            },

            addFriend(state, target) {
                state.userData.friends.push(target);
            },

            delMessage(state, index) {
                state.userData.currentMessages.splice(index, 1);
            },

            updateCurrentMessage(state, {index, time, post, oppositeID}) {
                let item = state.userData.currentMessages[index];
                //对比最后一条信息的时间，如果不超过一分钟就不显示时间
                let last = item.content.mList[item.content.mList.length - 1];
                let lastTime = new Date(last.time);
                if ((time - lastTime) / 1000 > 60) {
                    post.updatedTime = time.toISOString().slice(5, 10)
                        + ' ' + time.toTimeString().slice(0, 5);
                } else {
                    post.updatedTime = '';
                }

                item.content.mList.push(post);
                item.content.listLength += 1;

                //踢掉最前面的一条信息，最方便的方法，哈哈
                if (item.content.mList.length > 15) {
                    item.content.mList.shift();
                }

                state.userData.currentMessages.splice(index, 1);
                state.userData.currentMessages.unshift(item);

                if (state.chatTarget._id === oppositeID) {
                    state.chatMessage.push(post);
                    //因为更新了新信息所以现在已加载的步数加一
                    state.chatData.totalLength += 1;
                    //聊天窗口不用踢数据，加一就行
                    state.chatData.mLimit += 1;
                    //更新messagesActive
                    if (index !== 0) {
                        state.messageActive.fill(false);
                        state.messageActive[0] = true;
                    }
                } else {
                    item.unread += 1;
                    //更新messagesActive数组
                    let activeI = state.messageActive.indexOf(true);
                    if (activeI !== -1 && index > activeI) {
                        state.messageActive.pop();
                        state.messageActive.unshift(false);
                    }
                }
            },

            addNewCurrentMessage(state, {target, oppositeID, mID, unreadAdd}) {
                state.userData.currentMessages.unshift(target);
                if (this.chatTarget._id === oppositeID) {
                    //十分丑陋，但是不改了
                    this.chatData.mID = mID;
                    this.chatMessage = [...target.content.mList];
                    //active数组里增加一个
                    this.messageActive.unshift(true);
                } else if (unreadAdd !== 0) {
                    //增加未读信息条数
                    target.unread = 1;
                    //active数组里增加一个
                    this.messageActive.unshift(false);
                }
            },

            addMoreMessage(state, more) {
                state.chatData.mLimit += 15;
                state.chatMessage = [...more, ...state.chatMessage];
            },

            //更新头像
            updateProfile(state, {data, target}) {
                if (target === state.userData._id) {
                    state.userData.profile = data;
                } else {
                    for (let item of state.userData.friends) {
                        if (target === item._id) {
                            item.profile = data;
                            break;
                        }
                    }

                    for (let item of state.userData.currentMessages) {
                        if (item.opposite._id === target) {
                            item.opposite.profile = data;
                            break
                        }
                    }
                }
            }
        },
        actions: {

        }
    })
}